//
//  DemoCastle.m
//  TowerDefence
//
//  Created by  on 5/25/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "DemoCastle.h"
#import "PlayScreen.h"

@implementation DemoCastle


-(id)initDemoCastleWith:(GameLayer *)_gameLayer {
    self = [super initGameObjectWith:_gameLayer];
    spriteBody = [[CCSprite alloc] init];
    //[self.handler addGameObject:self];
    [self loadCastle];
    return self;
}
-(void)update:(ccTime)_timeUpdate {
    
}
-(void)loadCastle {
    
    NSMutableArray *frameAnimationArray = [[NSMutableArray alloc]init];
    for(int i = 0; i < 1; i ++) {
        for(int j=0; j<4; j++){
            CCSpriteFrame *spriteFrame = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"Images/PlayScreen/castle1.png%d%d",i, j]];
            [frameAnimationArray addObject:spriteFrame];
        }
    }
    
    CCAnimation *animation = [CCAnimation animationWithFrames:frameAnimationArray delay:0.5];
    //[spriteBody runAction:[CCAnimate actionWithAnimation:animation]];  
    // [spriteBody runAction:[CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:animation restoreOriginalFrame:NO]]];  
    
    
    //democastle = [CCSprite spriteWithFile:@"Images/demo_castle.png"];
    democastle01 = [CCSprite spriteWithSpriteFrameName:@"Images/PlayScreen/castle1.png00"];
   // democastle01.position = ccp(0,0);
    [democastle01 runAction:[CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:animation restoreOriginalFrame:NO]]];
    menuItemSpriteCastle = [CCMenuItemSprite itemFromNormalSprite:democastle01 selectedSprite:nil target:self selector:@selector(updateCastle)];
    // menuItemCastle = [CCMenuItem itemWithTarget:democastle selector:@selector(updateCastle)];
    menuCastle = [CCMenu menuWithItems:menuItemSpriteCastle, nil];
    menuCastle.position = ccp(0,0);
    [spriteBody addChild:menuCastle];
    
}

-(void)updateCastle {
    NSLog(@"Thao");
    temp = [CCSprite node];
    option = [CCSprite spriteWithFile:@"Images/PlayScreen/option01.png"];
    menuItemSpriteOption =[CCMenuItemSprite itemFromNormalSprite:option selectedSprite:nil target:self selector:@selector(sellCastleAnimation)];
    menuOption =[CCMenu menuWithItems:menuItemSpriteOption, nil];
    menuOption.position = ccp(0, 0);
    
    [temp addChild:menuOption];
    temp.scale = 0;
    [temp runAction:[CCSequence actions:[CCScaleTo actionWithDuration:0.2f scale:1.0f], nil]];
    [spriteBody addChild:temp]; 
}

-(void)sellCastleAnimation {
    CCCallFunc *sellCastleFunc = [CCCallFunc actionWithTarget:self selector:@selector(sellCastleCallback)];
    [temp runAction:[CCSequence actions:[CCScaleTo actionWithDuration:0.2f scale:0.0f], sellCastleFunc, nil]];
}

-(void)sellCastleCallback {
    [(PlayScreen *)handler sellTower:self];
}


@end
